package cn.zhangbo.config;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import cn.zhangbo.cons.LogTemplate;
import cn.zhangbo.dao.StudentMapper;
import cn.zhangbo.dao.TeacherMapper;
import cn.zhangbo.dao.po.Student;
import cn.zhangbo.dao.po.Teacher;
import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
@AutoConfigureAfter(AutoGenerateStudentConfiguration.class)
public class AutoGenerateTeacherConfiguration {
    TeacherMapper teacherMapper;
    StudentMapper studentMapper;
    List<Teacher> teachers;

    @Autowired
    AutoGenerateTeacherConfiguration(TeacherMapper teacherMapper, StudentMapper studentMapper) {
        this.teacherMapper = teacherMapper;
        this.studentMapper = studentMapper;
        initTeacher();
        doSave();
        log.info(String.format(LogTemplate.CONFIG_TEMPLATE, "自动生成10个老师"), AutoGenerateTeacherConfiguration.class);
    }

    public void initTeacher() {
        teachers = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            Teacher temp = new Teacher();
            temp.setId((long) i);
            temp.setSex(i ^ 1);
            temp.setAge(i);
            temp.setAddress("你知道我住哪吗" + i + (i ^ 2));
            temp.setCreateDatetime(new Date());
            temp.setEmail("swust" + i + "@163.com");
            temp.setName("student+" + i);
            temp.setStudents(assignStudentRandom(20));
            teachers.add(temp);
        }
    }

    public List<Student> assignStudentRandom(int numberOfStudent) {
        if (studentMapper != null && numberOfStudent > 0) {
            List<Student> partStudent = new ArrayList<Student>();
            List<Student> allStudent = studentMapper.findAll();
            Map<Long, Student> id2StudentMap = allStudent.stream()
                    .collect(Collectors.toMap(e -> e.getId(), e -> e));
            List<Long> idList = id2StudentMap.keySet()
                    .stream()
                    .sorted()
                    .collect(Collectors.toList());

            long minId = idList.get(0);
            long maxId = idList.get(idList.size() - 1);
            int i = 1;
            long id = (int) (Math.random() * (maxId - minId) + minId);
            while (i <= numberOfStudent) {
                Student temp = id2StudentMap.get(id);
                if (temp != null) {
                    partStudent.add(temp);
                    id2StudentMap.remove(id);
                    i++;
                }
                id = (int) (Math.random() * (maxId - minId) + minId);
            }
            return partStudent;
        }
        return Collections.emptyList();
    }

    public void doSave() {
        if (teacherMapper != null) {
            teacherMapper.save(teachers);
        }
    }
}
